//
//  SetPayViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/23.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "SetPayViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "UpdateSuccessViewController.h"
#import "YoungAllData.h"

@interface SetPayViewController ()<UITextFieldDelegate,YoungDelegate>
{
    UITextField *writeOldNum;
    UITextField *writePhoneNum;
    UITextField *writePhoneNum1;
}
@end

@implementation SetPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"修改支付密码";
    
    [self initUI];

}
#pragma mark - initUI
-(void)initUI{
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    UIImageView *loginPwdB = [[UIImageView alloc] initWithFrame:CGRectMake(30,50, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //loginPwdBG.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdB.backgroundColor = [UIColor whiteColor];
    loginPwdB.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdB.layer.borderWidth = 0.5;
    loginPwdB.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdB];
    
    
    writeOldNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdB.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    writeOldNum.placeholder = @"原支付密码";
    writeOldNum.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdB addSubview:writeOldNum];
    writeOldNum.delegate = self;
    
    
    UIImageView *loginPwdBG = [[UIImageView alloc] initWithFrame:CGRectMake(30,loginPwdB.bottom+10, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //loginPwdBG.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdBG.backgroundColor = [UIColor whiteColor];
    loginPwdBG.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG.layer.borderWidth = 0.5;
    loginPwdBG.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG];
    
    
    writePhoneNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    writePhoneNum.placeholder = @"新支付密码";
    writePhoneNum.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG addSubview:writePhoneNum];
    writePhoneNum.delegate = self;
    
    UIImageView *loginPwdBG1 = [[UIImageView alloc] initWithFrame:CGRectMake(30,loginPwdBG.bottom+10, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //loginPwdBG1.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdBG1.backgroundColor = [UIColor whiteColor];
    loginPwdBG1.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG1.layer.borderWidth = 0.5;
    loginPwdBG1.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG1];
    
    
    writePhoneNum1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    writePhoneNum1.placeholder = @"确认支付密码";
    writePhoneNum1.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG1 addSubview:writePhoneNum1];
    writePhoneNum1.delegate = self;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(20, loginPwdBG1.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure"] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure1"] forState:UIControlStateHighlighted];
    [leftBtn addTarget:self action:@selector(updatePassword) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:leftBtn];
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(leftBtn.right+20, loginPwdBG1.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateHighlighted];
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:rightBtn];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [backgroundView addGestureRecognizer:singleTapGestureRecognizer];

}
-(void)singleTap{
    [writeOldNum resignFirstResponder];
    [writePhoneNum resignFirstResponder];
    [writePhoneNum1 resignFirstResponder];
}
#pragma mark - UItextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 确认
-(void)updatePassword{
    if (writeOldNum.text.length<= 0 || writeOldNum.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入原支付密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (writePhoneNum.text.length<= 0 || writePhoneNum.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入新支付密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (writePhoneNum1.text.length<= 0 || writePhoneNum1.text == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请再次输入新支付密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    YoungAllData *data  = [[YoungAllData alloc]init];
    data.delegate = self;
    //[data updataPassword:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andLoginPass:writeOldNum.text andNewLoginPass:writePhoneNum.text andReLoginPass:writePhoneNum1.text];
    [data modifyTrade:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andOldTradePass:writeOldNum.text andNewTradePass:writePhoneNum.text andReNewTradePass:writePhoneNum1.text];
}
#pragma mark - 修改支付密码回调
-(void)modifytrade:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
        UpdateSuccessViewController *success = [[UpdateSuccessViewController alloc]init];
        [self.navigationController pushViewController:success animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }
}
#pragma mark - 修改支付密码回调失败
-(void)requistFail{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];

}
#pragma mark - 取消
-(void)clickRightBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 返回上一个界面
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
